package com.ruoyi.web.controller.UserLevel;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.Packets.domain.Packets;
import com.ruoyi.Packets.service.IPacketsService;
import com.ruoyi.UserLevel.domain.UserLevel;
import com.ruoyi.UserLevel.service.IUserLevelService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateTools;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 用户等级 信息操作处理
 * 
 * @author ruoyi
 * @date 2019-08-05
 */
@Controller
@RequestMapping("/UserLevel/userLevel")
public class UserLevelController extends BaseController
{
    private String prefix = "UserLevel/userLevel";
    
    @Autowired
	private IPacketsService packetsService;
	
	@Autowired
	private IUserLevelService userLevelService;
	
	@RequiresPermissions("UserLevel:userLevel:view")
	@GetMapping()
	public String userLevel()
	{
	    return prefix + "/userLevel";
	}
	
	/**
	 * 查询用户等级列表
	 */
	@RequiresPermissions("UserLevel:userLevel:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(UserLevel userLevel)
	{
		startPage();
        List<UserLevel> list = userLevelService.selectUserLevelList(userLevel);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出用户等级列表
	 */
	@RequiresPermissions("UserLevel:userLevel:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(UserLevel userLevel)
    {
    	List<UserLevel> list = userLevelService.selectUserLevelList(userLevel);
        ExcelUtil<UserLevel> util = new ExcelUtil<UserLevel>(UserLevel.class);
        return util.exportExcel(list, "userLevel");
    }
	
	/**
	 * 新增用户等级
	 */
	@GetMapping("/add")
	public String add(ModelMap mmap)
	{
		Packets packets=new Packets();
		List<Packets> list = packetsService.selectPacketsList(packets);
		mmap.put("packetsList", list);	
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存用户等级
	 */
	@RequiresPermissions("UserLevel:userLevel:add")
	@Log(title = "用户等级", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(UserLevel userLevel) {
		userLevel.setCreateDate(DateTools.getYMDhms());
		Double discount = userLevel.getDiscount();
		userLevel.setUpdateCondition("完成订单金额满" + discount + "元");
		return toAjax(userLevelService.insertUserLevel(userLevel));
	}

	/**
	 * 修改用户等级
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap)
	{
		UserLevel userLevel = userLevelService.selectUserLevelById(id);
		Packets packets=new Packets();
		List<Packets> list = packetsService.selectPacketsList(packets);
		mmap.put("userLevel", userLevel);
		mmap.put("packetsList", list);		
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存用户等级
	 */
	@RequiresPermissions("UserLevel:userLevel:edit")
	@Log(title = "用户等级", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(UserLevel userLevel)
	{		
		userLevel.setUpdateDate(DateTools.getYMDhms());
		return toAjax(userLevelService.updateUserLevel(userLevel));
	}
	
	/**
	 * 删除用户等级
	 */
	@RequiresPermissions("UserLevel:userLevel:remove")
	@Log(title = "用户等级", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(userLevelService.deleteUserLevelByIds(ids));
	}
	
}
